import router from './router'
import store from './store'
// 导航守卫
router.beforeEach(async(to, from, next) => {
  console.log(store.getters.name)
  console.log('当前', to)
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.name) {
        await store.dispatch('user/getUserInfo')
        const routes = await store.dispatch('permission/filterRoutes', store.state.user.userInfo.roles.menus)
        router.addRoutes([...routes, { path: '*', redirect: '/404', hidden: true }])
        next(to.path)
        return
      }
      console.log(11)
      next()
    }
  } else {
    const whiteList = ['/login', '/404']
    if (whiteList.indexOf(to.path) === -1) {
      next('/login')
    } else {
      next()
    }
  }
})
